<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人资料</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            margin-top: 30px;
        }
        .profile-card {
            max-width: 600px;
            margin: 0 auto;
        }
        .avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            background-color: #007bff;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 48px;
            margin: 0 auto 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>个人资料</h2>
        <button class="btn btn-outline-secondary" onclick="goHome()">返回主页</button>
    </div>

    <div class="card profile-card">
        <div class="card-header">
            <h5>基本信息</h5>
        </div>
        <div class="card-body">
            <div class="text-center mb-4">
                <div class="avatar">
                    <span id="avatarLetter">U</span>
                </div>
                <h4 id="userName">加载中...</h4>
                <p class="text-muted" id="userEmail">加载中...</p>
            </div>

            <div id="profileInfo">
                <div class="text-center">
                    <div class="spinner-border" role="status">
                        <span class="visually-hidden">加载中...</span>
                    </div>
                </div>
            </div>

            <div class="mt-4">
                <button class="btn btn-primary" onclick="showEditProfile()">编辑资料</button>
                <button class="btn btn-outline-secondary" onclick="changePassword()">修改密码</button>
            </div>
        </div>
    </div>

    <!-- 编辑资料模态框 -->
    <div class="modal fade" id="editProfileModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑个人资料</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="profileForm">
                        <div class="mb-3">
                            <label for="editUsername" class="form-label">用户名</label>
                            <input type="text" class="form-control" id="editUsername" readonly>
                        </div>
                        <div class="mb-3">
                            <label for="editEmail" class="form-label">邮箱</label>
                            <input type="email" class="form-control" id="editEmail">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveProfile()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 修改密码模态框 -->
    <div class="modal fade" id="changePasswordModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">修改密码</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="passwordForm">
                        <div class="mb-3">
                            <label for="oldPassword" class="form-label">当前密码</label>
                            <input type="password" class="form-control" id="oldPassword" required>
                        </div>
                        <div class="mb-3">
                            <label for="newPassword" class="form-label">新密码</label>
                            <input type="password" class="form-control" id="newPassword" required>
                        </div>
                        <div class="mb-3">
                            <label for="confirmPassword" class="form-label">确认新密码</label>
                            <input type="password" class="form-control" id="confirmPassword" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="savePassword()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 页面加载时获取用户信息
    document.addEventListener('DOMContentLoaded', function() {
        loadUserProfile();
    });

    function goHome() {
        window.location.href = '/';
    }

    function loadUserProfile() {
        fetch('/api/user/info')
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    displayUserProfile(data.data);
                } else {
                    showError('获取用户信息失败: ' + data.message);
                }
            })
            .catch(error => {
                showError('获取用户信息失败: ' + error.message);
            });
    }

    function displayUserProfile(user) {
        document.getElementById('userName').textContent = user.username;
        document.getElementById('userEmail').textContent = user.email || '未设置邮箱';
        document.getElementById('avatarLetter').textContent = user.username.charAt(0).toUpperCase();

        const profileInfoDiv = document.getElementById('profileInfo');
        profileInfoDiv.innerHTML = `
            <div class="row">
                <div class="col-sm-4"><strong>用户ID:</strong></div>
                <div class="col-sm-8">${user.id}</div>
            </div>
            <div class="row mt-2">
                <div class="col-sm-4"><strong>用户名:</strong></div>
                <div class="col-sm-8">${user.username}</div>
            </div>
            <div class="row mt-2">
                <div class="col-sm-4"><strong>邮箱:</strong></div>
                <div class="col-sm-8">${user.email || '未设置'}</div>
            </div>
            <div class="row mt-2">
                <div class="col-sm-4"><strong>注册时间:</strong></div>
                <div class="col-sm-8">${new Date().toLocaleDateString()}</div>
            </div>
        `;

        // 设置编辑表单的默认值
        document.getElementById('editUsername').value = user.username;
        document.getElementById('editEmail').value = user.email || '';
    }

    function showEditProfile() {
        new bootstrap.Modal(document.getElementById('editProfileModal')).show();
    }

    function changePassword() {
        document.getElementById('passwordForm').reset();
        new bootstrap.Modal(document.getElementById('changePasswordModal')).show();
    }

    function saveProfile() {
        // 简化处理，实际应该调用API保存用户资料
        alert('资料保存功能需要后端API支持');
        bootstrap.Modal.getInstance(document.getElementById('editProfileModal')).hide();
    }

    function savePassword() {
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;

        if (newPassword !== confirmPassword) {
            alert('新密码和确认密码不一致');
            return;
        }

        // 简化处理，实际应该调用API修改密码
        alert('密码修改功能需要后端API支持');
        bootstrap.Modal.getInstance(document.getElementById('changePasswordModal')).hide();
    }

    function showError(message) {
        const profileInfoDiv = document.getElementById('profileInfo');
        profileInfoDiv.innerHTML = `<div class="alert alert-danger">${message}</div>`;
    }
</script>
</body>
</html>
